<style>
.pure_table_extra { width: 100%; margin-top: 5px; }
.pure_table_extra th { height: 25px; line-height: 25px; }
.pure_table_extra img { cursor: pointer; margin-right: 2px; }
.pure_table_extra td { vertical-align: middle; }
.smaller span { font-size: 11px !important; padding: 0.2em 1em !important; }
</style>

<?php
$pg = Utils::get_param('pg', 1);
$data = $orm->get('pb_ecom_mthd_delivery', 't.title', 'ASC', $pg, SIZE, NULL);
?>
<button onclick="Url.redirect(__redmd, __sys + '.php?i=' + __id + '&__file=index_courier&pg=' + __data.p);" class="pure-button pure-button-primary">New Courier</button>
<table class="pure-table pure_table_extra" cellpadding=0 cellspacing=0>
  <thead>
    <tr>
      <th class="ui-widget-header">Courier</th>
      <th class="ui-widget-header" style="width: 120px;">Availability</th>
      <th class="ui-widget-header" style="width: 40px;">&nbsp;</th>
    </tr>
  </thead>
  <tbody id="data_body"></tbody>
</table>
<div>&nbsp;</div>
<div id="pagination"></div>

<script type="text/javascript">
  /*<![CDATA[*/
  var __data = JSON.parse('<?php echo addslashes(json_encode($data)); ?>');
  var __file = '<?php echo addslashes($app_data->param_file); ?>';
  var __requrl = '<?php echo Utils::get_requrl($app_data); ?>';
  var __redmd = '<?php echo addslashes($app_data->sys->redmd); ?>';
  var __sys = '<?php echo addslashes($app_data->sys->php_name); ?>';
  var __id = '<?php echo $app_data->param_id; ?>';
  
  $(function(){
    render();
  });

  function render() {
    $('#pagination').pagination({
      items: __data.t,
      itemsOnPage: __data.s,
      currentPage: __data.p,
      cssStyle: 'compact-theme',
      onPageClick: function(pageNumber, event) {
        return Url.redirect(__redmd, __sys + '.php?i=' + __id + '&pg=' + pageNumber);
      }
    });
    render_data();
  };
  
  function render_data() {
    $('#data_body').empty();
    for (var idx in __data.d) {
      var itm = __data.d[idx];
      $('#data_body').append(
        '<tr class="' + ((idx % 2 == 0) ? 'pure-table-even' : 'pure-table-odd') + '">' + 
          '<td>' + itm.title + '</td>' +
          '<td class="smaller"><input id="avail_' + itm.id + '" onclick="set_avail(' + itm.id + ', this.checked);" type="checkbox" ' + ((itm.status == 1) ? 'checked="checked"' : '' ) + '" /><label id="avail_txt_' + itm.id + '" for="avail_' + itm.id + '" style="margin-right: 0;">' + ((itm.status == 1) ? 'Available' : 'Unavailable' ) + '</label></td>' +
          '<td>' + 
            '<span style="cursor: pointer; float: left;" class="ui-button-icon-primary ui-icon ui-icon-pencil" onclick="Url.redirect(' + __redmd + ', \'' + __sys + '.php?i=' + __id + '&__file=index_courier&id=' + itm.id + '&pg=' + __data.p + '\');"></span>' +
            '<span style="cursor: pointer; float: left;" class="ui-button-icon-primary ui-icon ui-icon-closethick" onclick="delete_obj(' + itm.id + ');"></span>' +
          '</td>' + 
        '</tr>'
      );
    }
    if (__data.d.length == 0) {
      $('#data_body').append('<tr class="pure-table-even"><td colspan=3>None</td></tr>');
    }
    $('#data_body input[type=checkbox]').button();
  };
  
  function delete_obj(id) {
    var req = Utils.get_by_id(id, __data.d);
    req.__pg = __data.p;
    
    Ajax.request(
      __requrl + 'delete_obj',
      'req=' + Utils.encodeURI(JSON.stringify(req)),
      function(rslt) {
        __data = rslt;
        render();
      },
      { confirm: 1 }
    );
  };

  function set_avail(id, avail) {
    if (avail) {
      $('#avail_' + id)[0].checked = true;
      $('#avail_txt_' + id).empty();
      $('#avail_txt_' + id).append('<span class="ui-button-text">Available</span>');
      $('#avail_txt_' + id).addClass('ui-state-active');
    } else {
      $('#avail_' + id)[0].checked = false;
      $('#avail_txt_' + id).empty();
      $('#avail_txt_' + id).append('<span class="ui-button-text">Unavailable</span>');
      $('#avail_txt_' + id).removeClass('ui-state-active');
    }
    var req = Utils.get_by_id(id, __data.d);
    req.status = avail ? 1 : 0;
    Ajax.request(
      __requrl.replace(__file, 'index_courier') + 'submit_obj',
      'req=' + Utils.encodeURI(JSON.stringify(req)),
      function(rslt) {
      },
      { silent: 1 }
    );
  };
  /*]]>*/
</script>